import { IContext, IEntityLocalDataService, IParam } from '@/core';
import { IOAUTH_CLIENT_DETAILS } from '../interface/i-oauth-client-details';
import keys from '../oauth-client-details-keys';

/**
 * Oauth_client_detailsDTODTO辅助类
 * 基于template/src/api/{{appEntities}}/data-service/{{appEntities}}-help.ts.hbs生成
 *
 * @export
 * @class Oauth_client_detailsDTOHelp
 */
export class Oauth_client_detailsDTOHelp {
	/**
	 * 获取实体数据服务
	 *
	 * @static
	 * @param {IParam} context
	 * @return {*}  {Promise<IEntityLocalDataService<IOAUTH_CLIENT_DETAILS>>}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async getService(
		context: IParam
	): Promise<IEntityLocalDataService<IOAUTH_CLIENT_DETAILS>> {
		return App.getDataService('OAUTH_CLIENT_DETAILS', context);
	}

	/**
	 * 获取是否缓存
	 *
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static getEnableCache() {
		return false;
	}

	/**
	 * DTO转化成数据对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} source dto对象
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async toDataObj(context: IContext, source: IParam) {
		const _data: any = {};
		//  标识
		_data.client_id = source.client_id;
		//  名称
		_data.client_name = source.client_name;
		//  资源id集合
		_data.resource_ids = source.resource_ids;
		//  访问密钥
		_data.client_secret = source.client_secret;
		//  权限范围
		_data.scope = source.scope;
		//  支持的授权类型
		_data.authorized_grant_types = source.authorized_grant_types;
		//  客户端重定向URI
		_data.web_server_redirect_uri = source.web_server_redirect_uri;
		//  authorities
		_data.authorities = source.authorities;
		//  access_token_validity
		_data.access_token_validity = source.access_token_validity;
		//  refresh_token_validity
		_data.refresh_token_validity = source.refresh_token_validity;
		//  其他信息
		_data.additional_information = source.additional_information;
		//  自动批准授予权限
		_data.autoapprove = source.autoapprove;
		//  创建时间
		_data.create_time = source.create_time;
		//  DC
		_data.dc = source.dc;
		//  处理预置字段（以srf开头）
		if (source && Object.keys(source).length > 0) {
			Object.keys(source).forEach((key: string) => {
				if (key.startsWith('srf')) {
					_data[key] = source[key];
				}
			});
		}
		return _data;
	}

	/**
	 * 转化数据（DTO转化成数据对象）
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async toDataObjArray(context: IContext, data: any[]) {
		const _data: any[] = [];
		if (data && Array.isArray(data) && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				const tempdata = await this.toDataObj(context, data[i]);
				_data.push(tempdata);
			}
		}
		return _data;
	}

	/**
	 * 数据对象转化成DTO
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} source 数据对象
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async toDto(context: IContext, source: IParam) {
		const _data: any = {};
		const excludes: string[] = [];
		//  标识
		if (source.hasOwnProperty('client_id')) {
			_data.client_id = source.client_id;
		}
		//  名称
		if (source.hasOwnProperty('client_name')) {
			_data.client_name = source.client_name;
		}
		//  资源id集合
		if (source.hasOwnProperty('resource_ids')) {
			_data.resource_ids = source.resource_ids;
		}
		//  访问密钥
		if (source.hasOwnProperty('client_secret')) {
			_data.client_secret = source.client_secret;
		}
		//  权限范围
		if (source.hasOwnProperty('scope')) {
			_data.scope = source.scope;
		}
		//  支持的授权类型
		if (source.hasOwnProperty('authorized_grant_types')) {
			_data.authorized_grant_types = source.authorized_grant_types;
		}
		//  客户端重定向URI
		if (source.hasOwnProperty('web_server_redirect_uri')) {
			_data.web_server_redirect_uri = source.web_server_redirect_uri;
		}
		//  authorities
		if (source.hasOwnProperty('authorities')) {
			_data.authorities = source.authorities;
		}
		//  access_token_validity
		if (source.hasOwnProperty('access_token_validity')) {
			_data.access_token_validity = source.access_token_validity;
		}
		//  refresh_token_validity
		if (source.hasOwnProperty('refresh_token_validity')) {
			_data.refresh_token_validity = source.refresh_token_validity;
		}
		//  其他信息
		if (source.hasOwnProperty('additional_information')) {
			_data.additional_information = source.additional_information;
		}
		//  自动批准授予权限
		if (source.hasOwnProperty('autoapprove')) {
			_data.autoapprove = source.autoapprove;
		}
		//  创建时间
		if (source.hasOwnProperty('create_time')) {
			_data.create_time = source.create_time;
		}
		//  DC
		if (source.hasOwnProperty('dc')) {
			_data.dc = source.dc;
		}
		// 附加额外数据
		if (source && Object.keys(source).length > 0 && keys) {
			Object.keys(source).forEach((key: string) => {
				if (!keys.includes(key) && !excludes.includes(key)) {
					_data[key] = source[key];
				}
			});
		}
		return _data;
	}

	/**
	 * 转化数组(数据对象转化成DTO数组)
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam[]} data 数据对象
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async toDtoArray(context: IContext, data: IParam[]) {
		const _data: any[] = [];
		if (data && Array.isArray(data) && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				const tempdata = await this.toDto(context, data[i]);
				_data.push(tempdata);
			}
		}
		return _data;
	}

	/**
	 * 处理响应DTO对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async set(context: IContext, data: IParam) {
		const _data: IParam = await this.toDataObj(context, data);
		return _data;
	}

	/**
	 * 处理请求数据对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async get(context: IContext, data: IParam = {}) {
		return await this.toDto(context, data);
	}

	/**
	 * 获取缓存数据
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {string} srfkey 数据主键
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async getCacheData(context: IContext, srfkey: string) {
		return {};
	}

	/**
	 * 获取缓存数组
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async getCacheDataArray(context: IContext) {
		return [];
	}

	/**
	 * 设置缓存数据
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async setCacheData(context: IContext, data: IParam) {}

	/**
	 * 设置缓存数组
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {any[]} data 数据
	 * @return {*}
	 * @memberof Oauth_client_detailsDTOHelp
	 */
	public static async setCacheDataArray(context: IContext, data: any[]) {}
}
